package com.otitan.la.forest.industry.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.otitan.auth.framework.basepro.config.HasPermission;
import com.otitan.la.forest.industry.dto.EnterpriseDTO;
import com.otitan.la.forest.industry.dto.EnterpriseEntityDTO;
import com.otitan.la.forest.industry.dto.EnterpriseStatisticalDTO;
import com.otitan.la.forest.industry.service.EnterpriseService;
import com.otitan.la.forest.industry.utils.FileUtil;
import com.otitan.la.forest.industry.vo.EnterpriseEntityVO;
import com.otitan.la.forest.industry.vo.OutputStatisticalVO;
import com.otitan.webapp.framework.basepro.model.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 企业信息表
 *
 * @author jiacs
 * @email xxx
 * @date 2020-10-14 10:50:02
 */
@Slf4j
@Api(tags = "企业信息表API")
@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;


    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public DataResult add(@Valid @RequestBody EnterpriseEntityDTO dto, BindingResult bindingResult) {
        return enterpriseService.add(dto, bindingResult);
    }


    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    public DataResult batchDelete(@RequestBody @ApiParam(value = "ID集合") List<String> ids) {

        return enterpriseService.batchDelete(ids);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    public DataResult delete(@PathVariable("id") String id) {
        List<String> ids = new ArrayList<String>() {{
            add(id);
        }};
        return enterpriseService.batchDelete(ids);
    }


    @ApiOperation(value = "更新")
    @PutMapping("/update")
    public DataResult editor(@Valid @RequestBody EnterpriseEntityDTO dto, BindingResult bindingResult) {
        return enterpriseService.editor(dto, bindingResult);
    }

    @ApiOperation(value = "列表查询（分页）")
    @PostMapping("/findPage/{page}/{size}")
    public DataResult<Page<EnterpriseEntityVO>> findPageData(@PathVariable("page") long page,
                                                       @PathVariable("size") long size,
                                                       @RequestBody EnterpriseDTO enterprise) {
        return enterpriseService.findPageData(page, size, enterprise);
    }

    @ApiOperation(value = "根据ID查询")
    @GetMapping("/findOne/{id}")
    public DataResult<EnterpriseEntityVO> findOne(@PathVariable("id") String id) {
        return enterpriseService.findOne(id);
    }


    @ApiOperation(value = "企业龙头企业统计")
    @PostMapping("/statistical")
    public DataResult<Map<String, Object>> statistical(EnterpriseStatisticalDTO enterpriseStatisticalDTO) {

        return enterpriseService.statistical(enterpriseStatisticalDTO);
    }


    @ApiOperation(value = "导出")
    @PostMapping("/download")
    public void download(@RequestBody EnterpriseDTO enterpriseDTO, HttpServletResponse httpServletResponse) {
        enterpriseService.download(enterpriseDTO, httpServletResponse);
    }

    @ApiOperation(value = "导入")
    @PostMapping("/import")
    public DataResult upload(@RequestParam("file") MultipartFile file) {
        return enterpriseService.upload(file);
    }

    @ApiOperation(value = "下载模板")
    @PostMapping("/downloadTemplate")
    public void importFile( HttpServletResponse httpServletResponse) {
        FileUtil.importFile(httpServletResponse,FileUtil.enterFileName,FileUtil.enterExcelPath);
    }


    @HasPermission("open")
    @ApiOperation(value = "根据地区编码,企业信息获取企业")
    @PostMapping("/findPageByCode/{page}/{size}")
    public DataResult<Page<OutputStatisticalVO>> findPageByCodes(@PathVariable("page") long page,
                                                                @PathVariable("size") long size,
                                                                @RequestBody EnterpriseDTO enterpriseDTO) {
        return enterpriseService.findPageByCodes(page, size, enterpriseDTO);
    }
}